package com.simple.datax.transport.webservice.junit;

import java.util.List;

import org.junit.Test;

import com.simple.datax.transport.webservice.WebServiceConn;
import com.simple.datax.transport.webservice.WebServiceConnClient;

import junit.framework.TestCase;

public class WSTestCase extends TestCase {

	/**
	 * 测试数据准备:
	 * 1. 在TBL_WS_PROP新增EC111111数据，保证数据有效性。在输出目录中加入一些测试文件，保证程序能读。
	 * 测试文件内容与 COTENT一致。
	 * 2. 在TBL_WS_PROP新增EC333333数据，设置输入输出目录为无效目录
	 */
	public static final String CONTENT = "测试数据TestData";
	
	@Test
	public void testWSWithoutSSL() {
		WebServiceConn wsProxy = getConnection(false);
		String ret = wsProxy.sendData("EC111111", "", CONTENT);
		assertNull(ret);
		
		javax.xml.ws.Holder<List<String>> result = new javax.xml.ws.Holder<List<String>>();
		ret = wsProxy.getDataList("EC111111", "", result);
		assertNull(ret);
		assertTrue(result.value.size()>0);
		
		javax.xml.ws.Holder<byte[]> data = new javax.xml.ws.Holder<byte[]>();
		ret = wsProxy.getData("EC111111", "tester",
				result.value.get(result.value.size() - 1), data);
		assertNull(ret);
		assertTrue(new String(data.value).equals(CONTENT));
	}
	
	@Test
	public void testWSWithSSL() {
		WebServiceConn wsProxy = getConnection(true);
		String ret = wsProxy.sendData("EC111111", "", CONTENT);
		assertNull(ret);
		
		javax.xml.ws.Holder<List<String>> result = new javax.xml.ws.Holder<List<String>>();
		ret = wsProxy.getDataList("EC111111", "", result);
		assertNull(ret);
		assertTrue(result.value.size()>0);
		
		javax.xml.ws.Holder<byte[]> data = new javax.xml.ws.Holder<byte[]>();
		ret = wsProxy.getData("EC111111", "tester",
				result.value.get(result.value.size() - 1), data);
		assertNull(ret);
		assertTrue(new String(data.value).equals(CONTENT));
	}
	
	@Test
	public void testWSWithWrongParameters() {
		WebServiceConn wsProxy = getConnection(false);
		
		// 无效的报文
		String ret = wsProxy.sendData("EC111111", "", "");
		assertNotNull(ret);
		ret = wsProxy.sendData("EC111111", "", null);
		assertNotNull(ret);
		
		// 无效的返回值参数
		javax.xml.ws.Holder<List<String>> result = null;
		ret = wsProxy.getDataList("EC111111", "", result);
		// result 虽然为null，但是在web service server端此参数不为null
		// 所以server端程序正常处理，但客户端得不到返回值
		assertNull(ret);
		assertNull(result);
		
		javax.xml.ws.Holder<byte[]> data = null;
		ret = wsProxy.getData("EC111111", "tester", "NO_EXISTED.xml", data);
		assertNotNull(ret);
		assertNull(result);
		
		// 数据库目录数据无效
		ret = wsProxy.sendData("EC333333", "", CONTENT);
		assertNotNull(ret);
		ret = wsProxy.getDataList("EC333333", "", result);
		assertNotNull(ret);
		ret = wsProxy.getData("EC333333", "tester", "NO_EXISTED.xml", data);
		assertNotNull(ret);
	}
	
	@Test
	/**
	 * 企业用户不存在
	 */
	public void testWSWithUnknownEnterprise() {
		WebServiceConn wsProxy = getConnection(false);
		
		String ret = wsProxy.sendData("EC444444", "", CONTENT);
		assertNotNull(ret);
		javax.xml.ws.Holder<List<String>> result = new javax.xml.ws.Holder<List<String>>();
		ret = wsProxy.getDataList("EC444444", "", result);
		assertNotNull(ret);
		
		javax.xml.ws.Holder<byte[]> data = new javax.xml.ws.Holder<byte[]>();
		ret = wsProxy.getData("EC444444", "tester", "NO_EXISTED.xml", data);
		assertNotNull(ret);
	}
	
	private WebServiceConn getConnection(boolean ssl) {
		WebServiceConn wsProxy = null;
		if (ssl) {
			wsProxy = WebServiceConnClient.getWSProxy(
				"https://localhost:8443/datax_service/services/WebServiceConn",
				true);
		} else {
			wsProxy = WebServiceConnClient.getWSProxy(
				"http://localhost:8080/datax_service/services/WebServiceConn",
				false);
		}
		return wsProxy;
	}
}
